
package ch.hearc.cryptologie.tools;

public class CryptTools
	{
	
	public static int modpow(int b, int e, int m)
		{
		int r = 1;
		int tmp = 0;
		while(e > 0)
			{
			tmp = e & 1;
			if (tmp > 0)
				{
				r = (r * b) % m;
				}
			
			e = e >> 1;
			b = (b * b) % m;
			System.out.println(r + " , " + b);
			}
		
		return r;
		}
	
	public static int modinv(int n, int m)
		{
		int i2 = 0;
		int i1 = 1;
		int i = 0;
		int q = 0;
		int r = 0;
		int tmp = 0;
		while(true)
			{
			q = m / n;
			r = m % n;
			System.out.println("q = "+q);
			System.out.println("r = "+r);
			System.out.println("n = "+n);
			System.out.println("m = "+m);
			System.out.println("i2 = "+i2);
			System.out.println("i1 = "+i1);
			if (r == 0)
				{
				break;
				}
			
			i = i2 - q * i1;
			
			tmp = m;
			m = n;
			n = tmp;
			
			tmp = n;
			n = r;
			r = tmp;
			
			tmp = i2;
			i2 = i1;
			i1 = tmp;
			
			tmp = i1;
			i1 = i;
			i = tmp;
			}
		if (n != 1)
			{
			System.out.println("n et m non premiers entre eux !");
			}
		
		return i1;
		}
	
	public static int test(int u, int a, int b)
		{
		return (1+u*a)/b;
		}
	}
